package com.maiji.cloud.controller;

import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.UploadImageReqData;
import com.maiji.cloud.request.shopingmall.*;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.GoodsParamConfigResData;
import com.maiji.cloud.response.shopingmall.GoodsResData;
import com.maiji.cloud.response.shopingmall.ParamConfigResData;
import com.maiji.cloud.service.*;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

/**
 * 商品操作相关请求
 */
@RestController
@RequestMapping("/goods/")
public class GoodsController {
	Logger logger = LoggerFactory.getLogger(GoodsController.class);

	@Autowired
	private GoodsService goodsService;
    @Autowired
    private GoodsCollectionService goodsCollectionService;
    @Autowired
    private LabelMiddleGoodsService labelMiddleGoodsService;
    @Autowired
    private ParamConfigService paramConfigService;
    @Autowired
    private GoodsParamConfigService goodsParamConfigService;

	/**
	 * 添加商品
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("addGoods")
	public BaseResDto addGoods(@RequestBody BaseDataReqDto<GoodsReqData> baseDataReqDto) {
        try {
            GoodsReqData data = baseDataReqDto.getData();
            if (data == null || StringUtil.isBlank(data.getUserId())) return new BaseResDto(Status.PARAMETERERROR);
            return goodsService.addGoods(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

	/**
	 * 编辑商品
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("updateGoods")
	public BaseResDto updateGoods(@RequestBody BaseDataReqDto<GoodsReqData> baseDataReqDto) {
        try {
            GoodsReqData goodsReqData = baseDataReqDto.getData();
            if (StringUtil.isBlank(goodsReqData.getUuId()) || StringUtil.isBlank(goodsReqData.getUserId()))
                return new BaseResDto(Status.PARAMETERERROR);
            return goodsService.updateGoods(goodsReqData);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

	/**
	 * 编辑商品的标签
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("updateGoodsLabels")
	public BaseResDto updateGoodsLabels(@RequestBody BaseDataReqDto<LabelBelongUpdateReqData> baseDataReqDto) {
		String userId = baseDataReqDto.getData().getUserId();
		String goodsId = baseDataReqDto.getData().getBelongId();
		List<String> delLabelIds = baseDataReqDto.getData().getDelLabelIds();
		List<String> addLabelIds = baseDataReqDto.getData().getAddLabelIds();
		if (StringUtil.isBlank(userId) || StringUtil.isBlank(goodsId)) return new BaseResDto(Status.PARAMETERERROR);
		return goodsService.updateGoodsLabels(userId, goodsId, delLabelIds, addLabelIds);
	}

	/**
	 * 编辑商品的图片
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("updateGoodsImages")
	public BaseResDto updateGoodsImages(@RequestBody BaseDataReqDto<GoodsImagesUpdateReqData> baseDataReqDto) {
        String goodsId = baseDataReqDto.getData().getGoodsId();
        List<UploadImageReqData> goodsCarouselImgs = baseDataReqDto.getData().getGoodsCarouselImgs();
        if (StringUtil.isBlank(goodsId)) return new BaseResDto(Status.PARAMETERERROR);
		return goodsService.updateGoodsImages(goodsId, goodsCarouselImgs);
	}

	/**
	 * 根据商品ID获取商品的标签
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findAllLabelsByGoodsId")
	public BaseDataResDto<List<String>> findAllLabelsByGoodsId(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
		String goodsId = baseDataReqDto.getData();
		if (StringUtil.isBlank(goodsId))
			return new BaseDataResDto(Status.PARAMETERERROR);
		return goodsService.findAllLabelsByGoodsId(goodsId);
	}

	/**
	 * 根据商品ID查看商品详情
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findGoodsById")
	public BaseDataResDto<GoodsResData> findGoodsById(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                      @RequestHeader(name = "maijiToken", required = false) String token) {
		String goodsId = baseDataReqDto.getData();
		if (StringUtil.isBlank(goodsId)) return new BaseDataResDto(Status.PARAMETERERROR);
		return goodsService.findGoodsById(goodsId, token);
	}

	/**
	 * 获取商品列表
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findAllGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllGoods(@RequestBody BaseDataReqDto<GoodsFindAllReqData> baseDataReqDto) {
		GoodsFindAllReqData data = baseDataReqDto.getData();
		ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
		if (data == null || reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		return goodsService.findAllGoods(data, reqMetaData, null);
	}

	/**
	 * 根据标签ID查询商品列表
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findAllGoodsByLabelIds")
	public BaseMetaResDto<List<GoodsResData>> findAllGoodsByLabelIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
                                                                     @RequestHeader(value="maijiToken",required =false) String token) {
		List<String> labelIdList = baseDataReqDto.getData();
		ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
		if (labelIdList == null || labelIdList.size() == 0 || reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		GoodsFindAllReqData data = new GoodsFindAllReqData();
		return goodsService.findAllGoods(data.setLabelIds(labelIdList), reqMetaData, token);
	}

	/**
	 * 获取可积分兑换的商品列表_APP
	 * @param baseReqMetaDto
	 * @return
	 */
	@PostMapping("findAllExchangeGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllExchangeGoods(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                   @RequestHeader(value="maijiToken",required =false) String token) {
		ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
		if (reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        GoodsFindAllReqData data = new GoodsFindAllReqData().setIsExchange(1);
        return goodsService.findAllGoods(data, reqMetaData, token);
	}

	/**
	 * 获取可积分兑换的商品列表_PC
	 * @param baseReqMetaDto
	 * @return
	 */
	@PostMapping("findAllExchangeGoodsPC")
	public BaseMetaResDto<List<GoodsResData>> findAllExchangeGoodsPC(@RequestBody BaseDataReqDto<String> baseReqMetaDto) {
        ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
		if (reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		GoodsFindAllReqData data = new GoodsFindAllReqData().setName(baseReqMetaDto.getData()).setOrderBy("is_exchange");
        return goodsService.findAllGoods(data, reqMetaData, null);
	}

	/**
	 * 获取积分商城中新品推荐商品列表
	 * @param baseReqMetaDto
	 * @return
	 */
	@PostMapping("findAllNewExchangeGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllNewExchangeGoods(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                      @RequestHeader(value="maijiToken",required =false) String token) {
		ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
		if (reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		return goodsService.findAllNewExchangeGoods(reqMetaData, token);
	}

	/**
	 * 获取精选热卖商品列表
	 * @return
	 */
	@PostMapping("findAllRecommendGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllRecommendGoods(@RequestBody BaseDataReqDto<String> baseReqMetaDto,
                                                                    @RequestHeader(name = "maijiToken", required = false) String token) {
        String goodsName = baseReqMetaDto.getData();
        ReqMetaData metaData = baseReqMetaDto.getMetaData();
        return goodsService.findAllGoodsByLabel(goodsName, "精选热卖", metaData, token);
	}

	/**
	 * 获取新品推荐商品列表
	 * @return
	 */
	@PostMapping("findAllNewGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllNewGoods(@RequestBody BaseDataReqDto<String> baseReqMetaDto,
                                                                    @RequestHeader(name = "maijiToken", required = false) String token) {
        String goodsName = baseReqMetaDto.getData();
        ReqMetaData metaData = baseReqMetaDto.getMetaData();
        return goodsService.findAllGoodsByLabel(goodsName, "新品推荐", metaData, token);
	}

	/**
	 * 获取特惠专区商品列表
	 * @return
	 */
	@PostMapping("findAllPreferentialGoods")
	public BaseMetaResDto<List<GoodsResData>> findAllPreferentialGoods(@RequestBody BaseDataReqDto<String> baseReqMetaDto,
                                                                    @RequestHeader(name = "maijiToken", required = false) String token) {
	    System.out.println("============================ GoodsController >>>> " + Thread.currentThread().getName());
        String goodsName = baseReqMetaDto.getData();
        ReqMetaData metaData = baseReqMetaDto.getMetaData();
        return goodsService.findAllGoodsByLabel(goodsName, "特惠专区", metaData, token);
	}

	/**
	 * 快捷添加/取消商品标签
	 * @return
	 */
	@PostMapping("editLabelMidGoods")
	public BaseResDto editLabelMidGoods(@RequestBody BaseDataReqDto<GoodsReqData> baseReqMetaDto) {
        String goodsId = baseReqMetaDto.getData().getUuId();
        String labelName = baseReqMetaDto.getData().getLabelName();
        Boolean labelAdd = baseReqMetaDto.getData().getLabelAdd();
        if (StringUtil.isBlank(goodsId) || StringUtil.isBlank(labelName) || labelAdd == null)
            return new BaseResDto(Status.PARAMETERERROR);
        return labelMiddleGoodsService.editLabelMidGoods(goodsId, labelName, labelAdd);
	}

	/**
	 * 商品的收藏或取消收藏
	 * 
	 * @param findByIdReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("collectionGoodsById")
	public BaseResDto collectionGoodsById(@RequestBody FindByIdReqDto findByIdReqDto, @RequestHeader("maijiToken") String token) {
		String goodsId = findByIdReqDto.getData();
		if (StringUtil.isBlank(goodsId)) return new BaseResDto(Status.PARAMETERERROR);
		return goodsCollectionService.collectionGoodsById(goodsId, token);
	}

	/**
	 * 批量删除用户收藏商品（空参为清空收藏）
	 * @param baseDataReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("cancelCollectionGoods")
	public BaseResDto cancelCollectionGoods(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
                                            @RequestHeader("maijiToken") String token) {
		List<String> goodsIds = baseDataReqDto.getData();
		return goodsCollectionService.cancelCollectionGoods(goodsIds, token);
	}

	/**
	 * 获取用户收藏商品列表
	 * 
	 * @param baseReqMetaDto
	 * @param token
	 * @return
	 */
	@PostMapping("findAllGoodsCollection")
	public BaseMetaResDto<List<GoodsResData>> findAllGoodsCollection(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                     @RequestHeader("maijiToken") String token) {
		try {
			ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
			if (reqMetaData == null)
				return new BaseMetaResDto(Status.PARAMETERERROR);
			return goodsCollectionService.findAllGoodsCollection(token, reqMetaData);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseMetaResDto(Status.ERROR);
		}
	}

	/**
      *  获取食材相关商品列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllGoodsByFoodId")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto){
        String foodId = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        if (StringUtil.isBlank(foodId) || metaData == null) return new BaseMetaResDto<>(Status.ERROR);
        return goodsService.findAllGoodsByFoodIds(Arrays.asList(foodId), metaData);
    }

	/**
      *  获取食谱相关商品列表
     * @return
     */
    @PostMapping("findAllGoodsByFoodIds")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByFoodIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto){
        List<String> foodIds = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        if (foodIds == null || foodIds.size() == 0 || metaData == null) return new BaseMetaResDto<>(Status.ERROR);
        return goodsService.findAllGoodsByFoodIds(foodIds, metaData);
    }

    /**
     * 根据商品名称模糊匹配搜索
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllGoodsByName")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByName(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                 @RequestHeader("maijiToken") String token) {
        String goodsName = baseDataReqDto.getData();
        ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
        GoodsFindAllReqData goodsFindAllReqData = new GoodsFindAllReqData().setName(goodsName);
        return goodsService.findAllGoods(goodsFindAllReqData, reqMetaData, token);
    }

    /**
     * 根据商品ID[]获取关联商品列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllGoodsByIds")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        List<String> goodsIds = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        if (goodsIds == null || goodsIds.size() == 0) return new BaseMetaResDto(Status.PARAMETERERROR);
        GoodsFindAllReqData goodsFindAllReqData = new GoodsFindAllReqData().setGoodsIds(goodsIds);
        return goodsService.findAllGoods(goodsFindAllReqData, metaData, null);
    }

    /**
     * 统计需要及时增加库存的商品数量
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("IncreasestockVolumeCount")
    public BaseDataResDto<Integer> IncreasestockVolumeCount(@RequestBody BaseDataReqDto<Integer> baseDataReqDto) {
        Integer min = baseDataReqDto.getData();
        if (min == null || min <= 0) return new BaseDataResDto<>(Status.PARAMETERERROR);
        return goodsService.IncreasestockVolumeCount(min);
    }

    /**
     * 根据商品id获得产品参数
     * @param param
     * @return
     */
    @PostMapping("getGoodsParamByGoodsId")
    public BaseDataResDto<List<GoodsParamConfigResData>> getGoodsParamByGoodsId(@RequestBody BaseDataReqDto<String> param){
		String goodsId = param.getData();
		if(StringUtils.isBlank(goodsId)) return new BaseDataResDto<>(Status.PARAMETERERROR);
    	return goodsParamConfigService.getGoodsParamById(goodsId);
    }

   /**
    * 获得配置参数列表
    */
    @PostMapping("getParamConfigByGoodsId")
    public BaseDataResDto<List<ParamConfigResData>> getParamConfigByGoodsId(@RequestBody BaseDataReqDto<String> baseDataReqDto){
    	return paramConfigService.getParamConfigByGoodsId(baseDataReqDto.getData());
    }

   /**
    * 编辑商品参数类型
    */
    @PostMapping("editParamConfigs")
    public BaseDataResDto<List<ParamConfigResData>> editParamConfigs(@RequestBody BaseDataReqDto<List<ParamConfigReqData>> baseDataReqDto){
        List<ParamConfigReqData> data = baseDataReqDto.getData();
        if (data == null || data.size() == 0) return new BaseDataResDto(Status.ERROR);
    	return paramConfigService.editParamConfigs(data);
    }

   /**
    * 删除商品参数类型
    */
    @PostMapping("delParamConfigs")
    public BaseResDto delParamConfigs(@RequestBody BaseDataReqDto<String> baseDataReqDto){
        String pcId = baseDataReqDto.getData();
        if (StringUtil.isBlank(pcId)) return new BaseDataResDto(Status.ERROR);
    	return paramConfigService.delParamConfig(pcId);
    }
    
}
